<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sale:recommend:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sale:recommend:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sale:recommend:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sale:recommend:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recommendList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="编号" align="center" prop="id"/>
      <el-table-column label="商品名称" align="center" prop="productName"/>
        <el-table-column label="推荐状态" align="center" prop="recommendStatus">
          <template #default="{ row, $index }">
            <el-switch
              v-model="row.recommendStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="handleStatusChange(row)"
            />
          </template>
        </el-table-column>
        <el-table-column label="排序" align="center" prop="sort"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['sale:recommend:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改人气推荐商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-row>
        <el-input v-model="proParams.name" placeholder="请输入商品名称" clearable @keyup.enter.native="handleProQuery"/>
      </el-row>
      <el-table :data="productList" border ref="multipleTable" @selection-change="handleSelectionChangePro"
                height="500px">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column label="商品编号" align="center" prop="id" width="100"/>
        <el-table-column label="商品名称" align="center" prop="name"/>
        <el-table-column label="商品图片" align="center" prop="defaultImg">
          <template slot-scope="scope">
            <img :src="scope.row.defaultImg" width="50" height="50"/>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="verifyStatus">
          <template #default="{ row, $index }">
          <span style="color: #00afff" v-show="row.verifyStatus == 1">
           审核通过
          </span>
            <span style="color: red" v-show="row.verifyStatus == 0">
             未审核
            </span>
            <span style="color: red" v-show="row.verifyStatus == 2">
             审核未通过
            </span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="proTotal>0" :total="proTotal" :page.sync="proParams.pageNum" :limit.sync="proParams.pageSize"
                  @pagination="getProductList"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

     <el-dialog
      :title="title"
      :visible.sync="showUpdateSort"
      width="30%"
    >
      <el-row>
        设置排序:
        <!--     <input v-model="form.sort" type="number">-->
        <el-input v-model="form.sort" type="number" placeholder="请输入排序" clearable/>
      </el-row>
      <span slot="footer" class="dialog-footer">
    <el-button @click="showUpdateSort = false">取 消</el-button>
    <el-button type="primary" @click="sureUpdate">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {listRecommend, getRecommend, delRecommend, BatchAddRecommend, updateRecommend} from "@/api/sale/recommend";
import {listCommonProduct} from "@/api/common/common"

export default {
  name: "Recommend",
  data() {
    return {
      productList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 人气推荐商品表格数据
      recommendList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productName: null,
        recommendStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //商品参数
      proParams: {
        pageNum: 1,
        pageSize: 10,
        type: "hot"
      },
      proTotal: 0,
      //选中的商品
      SelectionProductData: [],
      showUpdateSort: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
      sureUpdate() {
      if (!this.form.sort) {
        this.$message.error('请输入排序')
        return
      }
      updateRecommend(this.form).then(res => {
        if (res.code == 200) {
          this.$message.success('修改成功')
          this.showUpdateSort = false
          this.getList()
        }
      })
    },
    handleStatusChange(row) {
      updateRecommend(row).then(res => {
        if (res.code == 200) {
          this.$message.success('修改成功')
          this.getList()
        }
      })
    },
    // 商品选择
    handleSelectionChangePro(val) {
      let flag = false;
      for (const item of val) {
        if (item.verifyStatus != 1) {
          this.$message.error('商品未通过审核');
          this.$refs.multipleTable.clearSelection();
          flag = true
          break; // 跳出循环
        }
      }
      if (!flag) {
        this.SelectionProductData = val
      }
    },
    /** 搜索按钮操作 */
    handleProQuery() {
      this.queryParams.pageNum = 1;
      this.getProductList();
    },
    getProductList() {
      listCommonProduct(this.proParams).then(response => {
        this.productList = response.rows;
        this.proTotal = response.total;
      });
    },
    /** 查询人气推荐商品列表 */
    getList() {
      this.loading = true;
      listRecommend(this.queryParams).then(response => {
        this.recommendList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productId: null,
        productName: null,
        recommendStatus: null,
        sort: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.getProductList();
      this.reset();
      this.open = true;
      this.title = "添加人气推荐商品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRecommend(id).then(response => {
        this.form = response.data;
        this.showUpdateSort = true;
        this.title = "修改人气推荐商品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.SelectionProductData.length == 0) {
        this.$message.error('请选择商品')
        return
      }
      let fromData = []
      this.SelectionProductData.forEach(item => {
        fromData.push({
          productId: item.id,
          productName: item.name,
          recommendStatus: 1,
          sort: 0
        })
      })
      console.log(fromData)
      BatchAddRecommend(fromData).then(res => {
        if (res.code == 200) {
          this.$modal.msgSuccess("添加成功");
          this.SelectionProductData = []
          this.open = false;
          this.getList();
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除人气推荐商品编号为"' + ids + '"的数据项？').then(function () {
        return delRecommend(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sale/recommend/export', {
        ...this.queryParams
      }, `recommend_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
